<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class Member extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
        $list=db('eb_member_ship')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
        $where=[];
        $list=db('eb_member_ship')->where($where)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        $inputdata=input();
        $res=db('eb_member_ship')->insert($inputdata);
        if($res){
            $this->success('添加成功',url('Member/index'));
        }else{
            $this->error('添加失败');
        }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }
    public function upload()
    {
        $file = request()->file('file');

        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀

        // 上传到七牛后保存的文件名
        $key = substr(md5($file->getRealPath()), 0, 5) . date('YmdHis') . rand(0, 9999) . '.' . $ext;

        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey = "6jDpNaFaqlbu4yaqIbNEVik5Xcu2idiNZKuR_WIy";
        $secretKey = "Pu3AqEZdNXSZk1vNks7hLelsKqXRw1fFcElqJXlZ";
        $auth = new Auth($accessKey, $secretKey);

        // 要上传的空间
        $bucket = "1810p";

        //域名
        $domain = "qv7d7onu9.hb-bkt.clouddn.com";
        $token = $auth->uploadToken($bucket);

        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();

        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);

        if ($err !== null) {
            return ["err" => 1, "msg" => $err, "data" => ""];
        } else {

            //返回图片的完整URL
            $imgPath = 'http://' . $domain . '/' . $key;
            //赋值
            return json(['code' => 0, 'data' => $imgPath, 'type' => 1, 'name' => $key]);

        }

    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
        $list=db('eb_member_ship')->where(['id'=>$id])->find();
        $where=array();
        $this->assign('list',$list);
        return $this->fetch();
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        $inputdata=input();
        $res=db('eb_member_ship')->where(['id'=>$id])->update($inputdata);
        if($res){
            $this->success('修改成功',url('Member/index'));
        }else{
            $this->error('修改失败');
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        $id= input('id');
        if (db('eb_member_ship')->where('id',$id)->delete()) {
            $this->success('删除成功',url("Member/index"));
        } else {
            $this->error('删除失败');
        }

    }

    public function indexs()
    {
        $title=input('title');
        $where =array();
        if ($title){
            $where['name']=['like','%'.$title.'%'];
        }
        $list=db('eb_member_card_batch')->where($where)->select();
        $this->assign('list',$list);
        $info=db('eb_member_card')->select();
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function type_index()
    {
        $info=db('eb_member_card')->select();
        $this->assign('info',$info);
        return $this->fetch();
    }
    public function creates()
    {
        return $this->fetch();
    }

    public function add(Request $request)
    {
        $inputdate=input();
     $list=db("eb_member_card_batch")->insert($inputdate);
        if($list){
            $this->success('添加成功',url('Member/indexs'));
        }else{
            $this->error('添加失败');
        }
    }


    public function memberes()
    {
        $list=db('record')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }


    public function rights()
    {
        $title=input('title');
        $where =array();
        if ($title){
            $where['status'==1]=['like','%'.$title.'%'];
        }
        $list=db('rights')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }


    public function rights_create()
    {
        //
        $where=[];
        $list=db('rights')->where($where)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function rights_save(Request $request)
    {
        //
        $inputdata=input();
        $res=db('rights')->insert($inputdata);
        if($res){
            $this->success('添加成功',url('Member/rights'));
        }else{
            $this->error('添加失败');
        }
    }

    public function rights_edit($id)
    {
        //
        $list=db('rights')->where(['id'=>$id])->find();
        $where=array();
        $this->assign('list',$list);
        return $this->fetch();
    }


    public function rights_update(Request $request, $id)
    {
        //
        $inputdata=input();
        $res=db('rights')->where(['id'=>$id])->update($inputdata);
        if($res){
            $this->success('修改成功',url('Member/rights'));
        }else{
            $this->error('修改失败');
        }
    }

    public function explan()
    {
        $title=input('title');
        $where =array();
        if ($title){
            $where['status'==1]=['like','%'.$title.'%'];
        }
        $list=db('explan')->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function explan_create()
    {
        //
        $where=[];
        $list=db('explan')->where($where)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }

    public function explan_save(Request $request)
    {
        //
        $inputdata=input();
        $res=db('explan')->insert($inputdata);
        if($res){
            $this->success('添加成功',url('Member/explan'));
        }else{
            $this->error('添加失败');
        }
    }

    public function explan_edit($id)
    {
        //
        $list=db('explan')->where(['id'=>$id])->find();
        $where=array();
        $this->assign('list',$list);
        return $this->fetch();
    }


    public function explan_update(Request $request, $id)
    {
        //
        $inputdata=input();
        $res=db('explan')->where(['id'=>$id])->update($inputdata);
        if($res){
            $this->success('修改成功',url('Member/explan'));
        }else{
            $this->error('修改失败');
        }
    }
    public function delete2($id)
    {
        //
        $id= input('id');
        if (db('explan')->where('id',$id)->delete()) {
            $this->success('删除成功',url("Member/explan"));
        } else {
            $this->error('删除失败');
        }

    }
    public function delete1($id)
    {
        //
        $id= input('id');
        if (db('rights')->where('id',$id)->delete()) {
            $this->success('删除成功',url("Member/rights"));
        } else {
            $this->error('删除失败');
        }

    }

}
